<?php

class AdminController extends CommonController{
	public function index() {
		$count = M ( $this->table )->count (); // 计算总数
		$p = new Page ( $count );
		$list = M ( array($this->table=>'a') )->field('a.*,r.name as role_name')->join('inner join '.C('DB_PREFIX').'role r on a.role_id=r.id')->limit ( $p->firstRow . ',' . $p->listRows )->select ();
		$this->assign ( "page", $p->show () );
		$this->assign ( "list", $list );
		$this->display ( $this->tpl );
	}
	public function saveBefore(){
		if($this->isGet()){
			$roleList=M('Role')->field('id,name')->select();
			$this->assign('roleList',$roleList);
		}
	}
	public function updateBefore(){
		$this->saveBefore();
		if($this->isPost()){
			if(empty($_POST['password'])){
				unset($_POST['password']);
			}else{
				$_POST['password']=md5($_POST['password']);
			}
		}
	}
	public function charge(){
		if($this->isGet()){
			$admin=M($this->table)->where('id='.intval($_GET['id']))->find();
			$this->assign('admin',$admin);
			$this->display();
		}else{
			begin();
			$money=floatval($_POST['money']);
			$id=intval($_POST['id']);
			$sql='update '.C('DB_PREFIX')."admin set money=money+{$money} where id={$id}";
			$logAdminMoney['admin_id']=$id;
			$logAdminMoney['money']=$money;
			$logAdminMoney['create_time']=$_SERVER['REQUEST_TIME'];
			$logAdminMoney['type']=$_POST['type'];
			$logAdminMoney['remark']=$_POST['remark'];
			if(zmysql_query($sql)&&M('LogAdminMoney')->save($logAdminMoney)){
				commit();
				$this->redirect('index');
			}else{
				rollback();
				$this->error('充值失败');
			}
		}
	}
}